<?php
class KenhxlsController extends AppController {

	var $name = 'Kenhxls';
	var $helpers = array('Html', 'Form');
    var $uses=array('Kenh','Kenhxl');

	function index() {
		$this->Kenhxl->recursive = 0;
		$this->set('kenhxls', $this->paginate('Kenhxl'));
	}
    function luu(){
        $kxl=$this->Kenhxl->find('all');
        $makenh=$this->Kenh->find('list',array('fields'=>array('makenh','makenh')));
        $succeed=0;
        foreach ($kxl as $k){
            if(in_array($k ['Kenhxl'] ['makenh'],$makenh)){
                $sql="update kenhs set 
                        loaikenh_id=".$k ['Kenhxl'] ['loaikenh_id'].",
                        tenkenh='".$k ['Kenhxl'] ['tenkenh']."', 
                        diachi='".$k ['Kenhxl'] ['diachi']."', 
                        sdt='".$k ['Kenhxl'] ['sdt']."',
                        donvi_id=".$k ['Kenhxl'] ['donvi_id']."
                    where makenh='".$k ['Kenhxl'] ['makenh']."' 
                    ";
                if(!$this->Kenhxl->query($sql)){
                    $this->Session->setFlash(__('Có lỗi khi sửa kênh!! Chỉ lưu được '.$succeed.' dòng đầu tiên', true));
                    $this->redirect(array('controller'=>'upfiles','action'=>'add'));
                }else{
                    $succeed++;
                }
            }
            else{
                $sql="
                    insert into kenhs (loaikenh_id,tenkenh,diachi,sdt,donvi_id,makenh) values( 
                        ".$k ['Kenhxl'] ['loaikenh_id'].",
                        '".$k ['Kenhxl'] ['tenkenh']."', 
                        '".$k ['Kenhxl'] ['diachi']."', 
                        '".$k ['Kenhxl'] ['sdt']."',
                        ".$k ['Kenhxl'] ['donvi_id'].",
                        '".$k ['Kenhxl'] ['makenh']."') 
                    ";
                debug($sql);
                if(!$this->Kenhxl->query($sql)){
                    $this->Session->setFlash(__('Có lỗi khi thêm kênh!! Chỉ lưu được '.$succeed.' dòng đầu tiên', true));
                    $this->redirect(array('controller'=>'upfiles','action'=>'add'));
                }else{
                    $succeed++;
                }
    


                    
            }
        }
        $this->Session->setFlash(__('IMPORT thành công!!', true));
        $this->redirect(array('controller'=>'upfiles','action'=>'add'));
    }

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Kenhxl.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('kenhxl', $this->Kenhxl->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Kenhxl->create();
			if ($this->Kenhxl->save($this->data)) {
				$this->Session->setFlash(__('The Kenhxl has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Kenhxl could not be saved. Please, try again.', true));
			}
		}
		$loaikenhs = $this->Kenhxl->Loaikenh->find('list');
		$donvis = $this->Kenhxl->Donvi->find('list');
		$this->set(compact('loaikenhs', 'donvis'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Kenhxl', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Kenhxl->save($this->data)) {
				$this->Session->setFlash(__('The Kenhxl has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Kenhxl could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Kenhxl->read(null, $id);
		}
		$loaikenhs = $this->Kenhxl->Loaikenh->find('list');
		$donvis = $this->Kenhxl->Donvi->find('list');
		$this->set(compact('loaikenhs','donvis'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Kenhxl', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Kenhxl->del($id)) {
			$this->Session->setFlash(__('Kenhxl deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>